package com.server.http.handler.impl;

import com.server.conditionenum.HttpReturnStatus;
import com.server.http.handler.HttpHandler;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.AppData;
import com.server.tools.HttpUtils;

/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年9月27日 上午9:41:19
*/
public class GetOpenIdERROR extends HttpHandler{
	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		String open_id = null;
        String failMsg = null;
		String js_code = request.getParameter("js_code");
		long appid = 0;
		try {
			appid = Long.parseLong(request.getParameter("appid"));
			if(AppData.appMap.containsKey(appid)) {
				String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + AppData.appMap.get(appid).wx_appid + "&secret=" + AppData.appMap.get(appid).wx_secret + "&js_code=" + js_code + "&grant_type=authorization_code";
				open_id = HttpUtils.GetHttpData(url,"","GET");
			}else {
				failMsg = "appid不存在!";
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			failMsg = "上传的参数错误!";
		}
		
		if(failMsg == null) {
			SetReturnData(response,open_id,HttpProtocol.GETOPENID,HttpReturnStatus.Successed);
		}else {
			SetReturnData(response,failMsg,HttpProtocol.GETOPENID,HttpReturnStatus.Failed);
		}
	}
}
